package com.itheima.web;

import com.itheima.result.R;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@RestController
public class SmsController {
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/sms")
    public R code(String phone, HttpSession session){
        //调用工具类生成一个4位的验证码，相应个给前端页面
        String code = ValidateCodeUtils.generateValidateCode4String(4);
        System.out.println(code);
        //SMSUtils.sendMessage("签名",);
        //把生成的验证码存入session作用域中，登录时候方便验证
        //session.setAttribute("code",code);
        String key = "login_" + phone;
        redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);

        return R.success(code);
    }
}
